<template>
	<div class="left-10 right-10">
		<div class="my-4 text-sm text-t-1 text-center">-- {{ $t('login.third') }} --</div>
		<div class="flex gap=[18px] justify-center">
			<base-button
				class="n-button--disabled !cursor-pointer basis-1/2"
				@click="thirdLoginHandle('google')">
				<template #icon>
					<base-image src="account/google.svg" />
				</template>
				{{ $t('login.google') }}
			</base-button>
		</div>
		<!-- <base-button class="mt-2" ghost>
			<template #icon>
				<base-image src="account/facebook.svg" />
			</template>
			{{ $t('login.facebook') }}
		</base-button> -->
	</div>
</template>
<script lang="ts" setup>
import { BaseButton, BaseImage } from '@/components/base'
import { AccountShowType } from '@/types'
import { useMessage } from '@/hooks'
import { thirdPartyRegister } from '@/api/account'
import { useAccountStore, useAppStore } from '@/store'
import HttpCode from '@/enums/http-code'
import { t } from '@/utils'

const appStore = useAppStore()
const accountStore = useAccountStore()
const message = useMessage()
const thirdLoginHandle = async (key: string) => {
	try {
		await accountStore.getThirdToken(key)
		await thirdPartySign(accountStore.thirdToken, key, 3)
	} catch (error) {}
}

const thirdPartySign = async (token: any, provider: any, deviceType: any) => {
	if (!appStore.uuid) {
		appStore.setUUID()
	}
	await signIn({
		// authToken: token,
		deviceType: deviceType,
		// deviceId: appStore.uuid,
		// provider: provider,
		// clientId: appStore.uuid,
		// grantType: 'Social',
		// socialCode: token,
		token: token,
		source: provider
	})
}

const signIn = async (data: any) => {
	try {
		const result: any = await thirdPartyRegister(data)
		if (result.data === null) {
			accountStore.showAccount(AccountShowType.thirdRegister)
		} else {
			await accountStore.thirdSignIn(result.data)
			// console.log(loginData)
			accountStore.showAccount(AccountShowType.none)
			message.success(t('login.success'))
			// window.location.reload()
		}
	} catch (error: any) {
		console.log()
		if (error.code === HttpCode.ThirdPartyAccountNotBind) {
			console.log(AccountShowType.register)
			accountStore.showAccount(AccountShowType.register)
		}
	}
}
</script>
